package model

import (
	"fmt"
	"gorm.io/gorm"
)

// 一分一段
type DivideSection struct {
	gorm.Model
	Score        any `json:"score" gorm:"type:varchar(255);default:null"`
	Num          any `json:"num" gorm:"type:varchar(255);default:null"`
	Total        any `json:"total" gorm:"type:varchar(255);default:null"`
	RankRange    any `json:"rank_range" gorm:"type:varchar(255);default:null"`
	BatchName    any `json:"batch_name" gorm:"type:varchar(255);default:null"`
	Controlscore any `json:"controlscore" gorm:"type:varchar(255);default:null"`
	Rank         any `json:"rank" gorm:"type:varchar(255);default:null"`
	Province     any `json:"province" gorm:"type:varchar(255);default:null"`
	Year         any `json:"year" gorm:"type:varchar(255);default:null"`
	ExamType     any `json:"exam_type" gorm:"type:varchar(255);default:null"`
}

func FirstDivideSection(divide DivideSection) (first DivideSection) {
	//fmt.Printf(">>>>省份代码：%s 年份：%s 文理综：%s  批次: %s \n", Provinces[provinceCode], year, ExamType, yearData.Level.One.NameToStr())
	where := map[string]interface{}{
		"province":   divide.Province,
		"year":       divide.Year,
		"score":      divide.Score,
		"exam_type":  divide.ExamType,
		"batch_name": divide.BatchName,
		"num":        divide.Num,
		"rank":       divide.Rank,
		"rank_range": divide.RankRange,
	}
	db.Where(where).First(&first)
	return
}

func CreateDivideSection(insert DivideSection) {
	fmt.Print("建：")
	db.Create(&insert)
}

func UpdateDivideSection(divide DivideSection) {
	fmt.Print("更：")
	where := map[string]interface{}{
		"province":   divide.Province,
		"year":       divide.Year,
		"score":      divide.Score,
		"exam_type":  divide.ExamType,
		"batch_name": divide.BatchName,
		"num":        divide.Num,
		"rank":       divide.Rank,
		"rank_range": divide.RankRange,
		"total":      divide.Total,
	}
	db.Where(where).Updates(divide)
}

func UpdateOrCreateDivideSection(where DivideSection) {
	first := FirstDivideSection(where)
	if first.ID == 0 {
		CreateDivideSection(where)
	} else {
		UpdateDivideSection(where)
	}
	fmt.Println(first.ID)
}

func MissOutDivideSection(year, province, exam_type, batch string) (first DivideSection) {
	where := map[string]interface{}{
		"province":   province,
		"year":       year,
		"exam_type":  exam_type,
		"batch_name": batch,
	}
	db.Where(where).First(&first)
	return
}
